package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.GenderType;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class RobotHandler implements CallbackHandler {

    @Resource
    protected RobotMapper robotMapper;

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        Robot robot = (Robot) new Robot()
                .setTrueName(data.getInteger("nTrueName"))
                .setQrcode(data.getString("vcPersonQRCode"))
                .setPhone(data.getString("vcPhone"))
                .setId(callback.getRobotId())
                .setGender(GenderType.valueOf(data.getInteger("nSex")))
                .setAlias(data.getString("vcWxAlias"))
                .setName(data.getString("vcNickName"))
                .setBase64Name(data.getString("vcBase64NickName"))
                .setAvatar(data.getString("vcHeadImgUrl"))
                .setWhatsUp(data.getString("vcWhatsUp"))
                .setBase64WhatsUp(data.getString("vcBase64WhatsUp"))
                .setProvince(data.getString("vcProvince"))
                .setCity(data.getString("vcCity"))
                .setCounty(data.getString("vcCountry"));
        robotMapper.insertOrUpdate(robot);
    }
}
